import os
import subprocess
import threading

from selenium import webdriver as wb
# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait as wdw
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pyautogui

from exe_js import js1,js3,js4,js5


def openpro():
    cmd = 'chrome.exe https://login.taobao.com/member/login.jhtml --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"'
    ret = subprocess.call(cmd)


def __login__(username, password, pathA, pathB):

    t2 = threading.Thread(target=openpro, args=())
    t2.setDaemon(True)
    t2.start()


    pyautogui.PAUSE = 3  # 设置每个动作0.2s太快来不及输入密码
    options = wb.ChromeOptions()
    #options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 切换到开发者模式
    #options.excludeSwitches='enable-automation'
    options.debugger_address = "localhost:9222"
    browser = wb.Chrome(options=options)
    browser.maximize_window()  # 窗口最大化保证坐标正确
    #browser.get('https://login.taobao.com/member/login.jhtml')
    # try:
    # left,top,width,height=pyautogui.locateOnScreen('G:/jupyter project/淘宝/login_switch_blue.PNG')
    # except:
    # left,top,width,height=pyautogui.locateOnScreen('G:/jupyter project/淘宝/login_switch.PNG')      获取login_switch位置
    browser.execute_script(js1)
    browser.execute_script(js3)
    browser.execute_script(js4)
    browser.execute_script(js5)
    pyautogui.moveTo(1400, 620)  # 移动到切换登录的位置
    pyautogui.click()  # 点击切换按钮
    pyautogui.typewrite(username)
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.press('backspace')
    pyautogui.typewrite(username)


    pyautogui.press('tab')
    pyautogui.typewrite(password)
    errorType = 0
    try:
        left, top, width, height = pyautogui.locateOnScreen(pathA)
        print('识别蓝色')
        moveToX = left + 27
        moveToY = top + 25
        print(moveToX, moveToY)
        pyautogui.moveTo(moveToX, moveToY)
        pyautogui.mouseDown()
        moveToX = moveToX + 325
        pyautogui.moveTo(moveToX, moveToY)
        pyautogui.mouseUp()
        #pyautogui.moveTo(moveToX - 250, moveToY + 60)
        #pyautogui.mouseDown()
        #pyautogui.mouseUp()
    except:
        errorType = 1  # 识别不出蓝色

    if errorType == 1:
        try:
            left, top, width, height = pyautogui.locateOnScreen(pathB)
            moveToX = left + 110
            moveToY = top + 13
            print('识别绿色')
            print(moveToX, moveToY)  # 1299 497
            pyautogui.moveTo(moveToX, moveToY)
            pyautogui.mouseDown()
            moveToX = moveToX + 300
            pyautogui.moveTo(moveToX, moveToY)
            pyautogui.mouseUp()
            pyautogui.moveTo(moveToX - 250, moveToY + 60)
            pyautogui.mouseDown()
            pyautogui.mouseUp()
        except:
            errorType = 2  # 识别不出绿色

    if errorType == 2:
        print('没有滑块')
        pyautogui.moveTo(1400, 620)
        pyautogui.mouseDown()
        pyautogui.mouseUp()

    return browser  # 返回浏览器当前的页面


if __name__ == '__main__':
    print(os.getcwd())
    browser = __login__('qishucloud@163.com', '230563hu~~',os.path.join(os.getcwd(),'hkbtn.png'), os.path.join(os.getcwd(),'hkbtn2.png'))